/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.PostpViewState;

/**
 *
 * @author  Samuel Silva Penna
 */
public class DisplayDialog extends JFrame {
    
	/**   */
	private static final long serialVersionUID = 1L;
	
	private DesktopPostp desktop;
	private JInternalFrame intFrame;
	private PostpViewState state;
    private JButton viewButton;
    private JButton okButton;
    private JCheckBox meshOnOffCheckBox;
    private JCheckBox materialOnOffCheckBox;
    private JCheckBox elementsLoadsCheckBox;
    private JCheckBox rangeScaleCheckBox;
    private JCheckBox discontinuosContoursCheckBox;
    private JCheckBox meshStateOnOffCheckBox;
    private JCheckBox meshContoursStateCheckBox;
    private JCheckBox restraintsCheckBox;
    private JCheckBox nodesOnOffCheckBox;
    private JCheckBox nodeLabelsCheckBox;
    private JCheckBox nodesCoordsCheckBox;
    private JCheckBox gaussMarksOnOffCheckBox;
    private JCheckBox gaussMeshOnOffCheckBox;
    private JCheckBox elementsLabelsCheckBox;
    private JCheckBox meshContoursCheckBox;
    private JCheckBox nodalsLoadsCheckBox;
    private JPanel mainPanel;
    private JPanel meshPanel;
    private JPanel nodesAndGaussPanel;
    private JPanel elementsPanel;
    private JPanel resultsPanel;
    private JPanel attributesPanel;
	
	
    public DisplayDialog(DesktopPostp desktop,ViewState view) {
    	this.desktop = desktop;
    	this.intFrame = (InterfacePostp)this.desktop.getDesktop().getSelectedFrame();
    	this.state = (PostpViewState)view;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    /**
     * 
     */
    private void initComponents() {
    	
    	this.setSize(new Dimension(385,410));
    	this.setPreferredSize(new Dimension(385,410));
    	this.setMaximumSize(new Dimension(385,410));
    	this.setMinimumSize(new Dimension(385,410));
    	this.setResizable(false);
    	this.setTitle("Display "+((InterfacePostp)this.intFrame).getPostpModel().getLabel());
    	this.setLocationRelativeTo(this.desktop);
    	Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
    	getContentPane().setLayout(null);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        mainPanel = new JPanel();
        mainPanel.setLayout(null);
        mainPanel.setBorder(BorderFactory.createTitledBorder("Set to Display"));
        
        //Setting the meshPanel
        meshPanel = new JPanel();
        meshPanel.setLayout(null);
        meshPanel.setBorder(BorderFactory.createTitledBorder(
        		new LineBorder(new Color(204, 204, 204), 1, true), "Mesh"));
        
        meshOnOffCheckBox = new JCheckBox();
        meshOnOffCheckBox.setText("Mesh On/Off");
        meshPanel.add(meshOnOffCheckBox);
        meshOnOffCheckBox.setBounds(10, 20, 120, 15);

        meshStateOnOffCheckBox = new JCheckBox();
        meshStateOnOffCheckBox.setText("Mesh State On/Off");
        meshPanel.add(meshStateOnOffCheckBox);
        meshStateOnOffCheckBox.setBounds(200, 20, 140, 15);

        //Setting the nodesAndGaussPanel
        nodesAndGaussPanel = new JPanel();
        nodesAndGaussPanel.setLayout(null);
        nodesAndGaussPanel.setBorder(BorderFactory.createTitledBorder(
        		new LineBorder(new Color(204, 204, 204), 1, true), "Nodes and Gauss Points"));
        
        nodesOnOffCheckBox = new JCheckBox();
        nodesOnOffCheckBox.setText("Nodes On/Off");
        nodesAndGaussPanel.add(nodesOnOffCheckBox);
        nodesOnOffCheckBox.setBounds(10, 20, 150, 15);

        nodesCoordsCheckBox = new JCheckBox();
        nodeLabelsCheckBox = new JCheckBox();
        nodeLabelsCheckBox.setText("Nodes Labels On/Off");
        nodesAndGaussPanel.add(nodeLabelsCheckBox);
        nodeLabelsCheckBox.setBounds(10, 40, 170, 15);

        nodesCoordsCheckBox.setText("Nodes Coords On/Off");
        nodesAndGaussPanel.add(nodesCoordsCheckBox);
        nodesCoordsCheckBox.setBounds(10, 60, 170, 15);
        
        gaussMarksOnOffCheckBox = new JCheckBox();
        gaussMarksOnOffCheckBox.setText("Gauss Marks On/Off");
        nodesAndGaussPanel.add(gaussMarksOnOffCheckBox);
        gaussMarksOnOffCheckBox.setBounds(200, 20, 140, 15);

        gaussMeshOnOffCheckBox = new JCheckBox();
        gaussMeshOnOffCheckBox.setText("Gauss Mesh On/Off");
        nodesAndGaussPanel.add(gaussMeshOnOffCheckBox);
        gaussMeshOnOffCheckBox.setBounds(200, 40, 140, 15);

        //Setting the elementPanel
        elementsPanel = new JPanel();
        elementsPanel.setLayout(null);
        elementsPanel.setBorder(BorderFactory.createTitledBorder(
        		new LineBorder(new Color(204, 204, 204), 1, true), "Elements"));
        
        elementsLabelsCheckBox = new JCheckBox();
        elementsLabelsCheckBox.setText("Elements Labels On/Off");
        elementsPanel.add(elementsLabelsCheckBox);
        elementsLabelsCheckBox.setBounds(10, 20, 170, 15);

        materialOnOffCheckBox = new JCheckBox();
        materialOnOffCheckBox.setText("Material On/Off");
        elementsPanel.add(materialOnOffCheckBox);
        materialOnOffCheckBox.setBounds(200, 20, 140, 15);

        //Setting resultsPanel
        resultsPanel = new JPanel();
        resultsPanel.setLayout(null);
        resultsPanel.setBorder(BorderFactory.createTitledBorder(
        		new LineBorder(new Color(204, 204, 204), 1, true), "Results"));
       
        meshContoursCheckBox = new JCheckBox();
        meshContoursCheckBox.setText("Mesh Contours On/Off");
        resultsPanel.add(meshContoursCheckBox);
        meshContoursCheckBox.setBounds(10, 20, 170, 15);

        meshContoursStateCheckBox = new JCheckBox();
        meshContoursStateCheckBox.setText("Mesh State Contours On/Off");
        resultsPanel.add(meshContoursStateCheckBox);
        meshContoursStateCheckBox.setBounds(10, 40, 180, 15);

        discontinuosContoursCheckBox = new JCheckBox();
        discontinuosContoursCheckBox.setText("Discontinuos Contours On/Off");
        resultsPanel.add(discontinuosContoursCheckBox);
        discontinuosContoursCheckBox.setBounds(200, 20, 140, 15);
        
        rangeScaleCheckBox = new JCheckBox();
        rangeScaleCheckBox.setText("Range Scale On/Off");
        resultsPanel.add(rangeScaleCheckBox);
        rangeScaleCheckBox.setBounds(200, 40, 140, 15);

        //Settings the attributePanel
        attributesPanel = new JPanel();
        attributesPanel.setLayout(null);
        attributesPanel.setBorder(BorderFactory.createTitledBorder(
        		new LineBorder(new Color(204, 204, 204), 1, true), "Attributes"));
       
        nodalsLoadsCheckBox = new JCheckBox();
        nodalsLoadsCheckBox.setText("Nodals Loads");
        attributesPanel.add(nodalsLoadsCheckBox);
        nodalsLoadsCheckBox.setBounds(10, 20, 150, 15);

        elementsLoadsCheckBox = new JCheckBox();
        elementsLoadsCheckBox.setText("Elements Loads");
        attributesPanel.add(elementsLoadsCheckBox);
        elementsLoadsCheckBox.setBounds(10, 40, 150, 15);

        restraintsCheckBox = new JCheckBox();
        restraintsCheckBox.setText("Restraints");
        attributesPanel.add(restraintsCheckBox);
        restraintsCheckBox.setBounds(200, 20, 140, 15);

        //Setting the mainPanel
        mainPanel.add(meshPanel);
        meshPanel.setBounds(10, 20, 350, 40);
        
        mainPanel.add(nodesAndGaussPanel);
        nodesAndGaussPanel.setBounds(10, 60, 350, 90);
        
        mainPanel.add(elementsPanel);
        elementsPanel.setBounds(10, 150, 350, 50);
        
        mainPanel.add(resultsPanel);
        resultsPanel.setBounds(10, 200, 350, 70);
        
        mainPanel.add(attributesPanel);
        attributesPanel.setBounds(10, 270, 350, 60);

        getContentPane().add(mainPanel);
        mainPanel.setBounds(5, 0, 370, 340);
        
        //Setting the buttons
        viewButton = new JButton();
        viewButton.setText("View");
        getContentPane().add(viewButton);
        viewButton.setBounds(110, 345, 80, 25);

        okButton = new JButton();
        okButton.setText("OK");
        getContentPane().add(okButton);
        okButton.setBounds(195, 345, 80, 25);
        
        viewButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						view(event);
					}
				});
        
        okButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						view(event);
						close(event);
					}
				});
        
        setUp();
        
    }                       
    
    public void close(ActionEvent event){
		this.dispose();
	}
    
    public void view(ActionEvent event){
		this.state.getMeshStateViewState().setMeshOnOff(this.meshOnOffCheckBox.isSelected());
		this.state.getMeshStateViewState().setMeshStateOnOff(this.meshStateOnOffCheckBox.isSelected());
		this.state.getVertexViewState().setVertexOnOff(this.nodesOnOffCheckBox.isSelected());
		this.state.getVertexViewState().setLabelOn(this.nodeLabelsCheckBox.isSelected());
		this.state.getVertexViewState().setCoordsOn(this.nodesCoordsCheckBox.isSelected());
		this.state.getVertexViewState().setRestraintsOnOff(this.restraintsCheckBox.isSelected());
		this.state.getVertexViewState().setLoadOnOff(this.nodalsLoadsCheckBox.isSelected());
		this.state.getGaussVertexViewState().setGaussPointsOn(this.gaussMarksOnOffCheckBox.isSelected());
		this.state.getGaussVertexViewState().setGaussMeshOn(this.gaussMeshOnOffCheckBox.isSelected());
		this.state.getFaceViewState().setLabelOn(this.elementsLabelsCheckBox.isSelected());
		if(this.materialOnOffCheckBox.isSelected()){
			this.state.getFaceViewState().setFillType("Transparent");
		}else{
			this.state.getFaceViewState().setFillType("NONE");
		}
		
		if(this.meshContoursCheckBox.isSelected()||this.meshContoursStateCheckBox.isSelected()||this.rangeScaleCheckBox.isSelected()){
			if(this.state.getElementsPlotContoursViewState().isElementsContoursComputed()){
				this.state.getElementsPlotContoursViewState().setContourOnOff(this.meshContoursCheckBox.isSelected());
				this.state.getElementsPlotContoursViewState().setContourMeshStateOnOff(this.meshContoursStateCheckBox.isSelected());
				this.state.getScaleColorsViewState().setScaleContoursOnOff(this.rangeScaleCheckBox.isSelected());
			}
			if(this.state.getNodalsPlotContoursViewState().isNodalsContoursComputed()){
				this.state.getNodalsPlotContoursViewState().setContourOnOff(this.meshContoursCheckBox.isSelected());
				this.state.getNodalsPlotContoursViewState().setContourMeshStateOnOff(this.meshContoursStateCheckBox.isSelected());
				this.state.getScaleColorsViewState().setScaleContoursOnOff(this.rangeScaleCheckBox.isSelected());
			}
			if(this.state.getElementsPlotContoursViewState().isElementsDiscontinuosContoursComputed()){
				this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(this.discontinuosContoursCheckBox.isSelected());
			}
//			else{
//				JOptionPane.showMessageDialog(this, "You must set the plot contours first!", "Error Message", JOptionPane.ERROR_MESSAGE);
//			}
		}else{
			this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(this.discontinuosContoursCheckBox.isSelected());
			this.state.getElementsPlotContoursViewState().setContourOnOff(this.meshContoursCheckBox.isSelected());
			this.state.getElementsPlotContoursViewState().setContourMeshStateOnOff(this.meshContoursStateCheckBox.isSelected());
			this.state.getScaleColorsViewState().setScaleContoursOnOff(this.rangeScaleCheckBox.isSelected());
			this.state.getNodalsPlotContoursViewState().setContourOnOff(this.meshContoursCheckBox.isSelected());
			this.state.getNodalsPlotContoursViewState().setContourMeshStateOnOff(this.meshContoursStateCheckBox.isSelected());
			this.state.getScaleColorsViewState().setScaleContoursOnOff(this.rangeScaleCheckBox.isSelected());
		}
		
		//Setar os atributos nodais
		
		((InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame()).getView().getDrawingArea().recompose();
		((InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame()).getView().getDrawingArea().repaint();
		
	}
         
    public void setUp(){
    	this.meshOnOffCheckBox.setSelected(this.state.getMeshStateViewState().isMeshOnOff());
    	this.meshStateOnOffCheckBox.setSelected(this.state.getMeshStateViewState().isMeshStateOnOff());
    	this.nodesOnOffCheckBox.setSelected(this.state.getVertexViewState().isVertexOnOff());
    	this.nodeLabelsCheckBox.setSelected(this.state.getVertexViewState().isLabelOn());
    	this.nodesCoordsCheckBox.setSelected(this.state.getVertexViewState().isCoordsOn());
    	this.restraintsCheckBox.setSelected(this.state.getVertexViewState().isRestraintsOnOff());
    	this.nodalsLoadsCheckBox.setSelected(this.state.getVertexViewState().isLoadOnOff());
    	this.gaussMarksOnOffCheckBox.setSelected(this.state.getGaussVertexViewState().isGaussPointsOn());
    	this.gaussMeshOnOffCheckBox.setSelected(this.state.getGaussVertexViewState().isGaussMeshOn());
    	this.elementsLabelsCheckBox.setSelected(this.state.getFaceViewState().isLabelOn());
    	if(this.state.getFaceViewState().getFillType()=="Transparent"||this.state.getFaceViewState().getFillType()=="Opaque"){
			this.materialOnOffCheckBox.setSelected(true);
		}
    	if(this.state.getElementsPlotContoursViewState().isElementsContoursComputed()){
    		this.meshContoursCheckBox.setSelected(this.state.getElementsPlotContoursViewState().isContourOnOff());
    		this.meshContoursStateCheckBox.setSelected(this.state.getElementsPlotContoursViewState().isContourMeshStateOnOff());
    		this.rangeScaleCheckBox.setSelected(this.state.getScaleColorsViewState().isScaleContoursOnOff());
    	} 
    	if(this.state.getNodalsPlotContoursViewState().isNodalsContoursComputed()){
    		this.meshContoursCheckBox.setSelected(this.state.getNodalsPlotContoursViewState().isContourOnOff());
    		this.meshContoursStateCheckBox.setSelected(this.state.getNodalsPlotContoursViewState().isContourMeshStateOnOff());
    		this.rangeScaleCheckBox.setSelected(this.state.getScaleColorsViewState().isScaleContoursOnOff());
    	} 
    	if(this.state.getElementsPlotContoursViewState().isElementsDiscontinuosContoursComputed()){
    		this.discontinuosContoursCheckBox.setSelected(this.state.getElementsPlotContoursViewState().isDiscontinuosContourOnOff());
    		//this.meshContoursStateCheckBox.setSelected(this.state.getElementsPlotContoursViewState().isContourMeshStateOnOff());
    		this.rangeScaleCheckBox.setSelected(this.state.getScaleColorsViewState().isScaleContoursOnOff());
    	}
    	//else{
    	//	JOptionPane.showMessageDialog(this, "You must set the plot contours first!", "Error Message", JOptionPane.ERROR_MESSAGE);
    	//}	
    	
    }
    
//*************************************************************************************************************
}//end of this class
